package com.example.chat.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chat.beans.Content;
import com.example.chat.mapper.ContentMapper;
import com.example.chat.result.ApiResult;
import com.example.chat.result.Base;
import com.example.chat.result.ContentResult;
import com.example.chat.result.ListResult;
import com.example.chat.utils.CheckUtils;
import com.example.chat.utils.Global;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ContentService extends ServiceImpl<ContentMapper, Content> {
    @Resource
    CheckUtils checkUtils;

    public ListResult<List<ContentResult>> listContent(String title, Integer page, Integer limit) {
        ListResult<List<ContentResult>> result = new ListResult<>();
        int status = 0;
        String msg = "获取失败";
        try {
            Page<Content> pages = new Page<>(page, limit);
            IPage<Content> iPages = baseMapper.selectBySearch(pages, title);
            List<ContentResult> results = new ArrayList<>();
            for (Content target : iPages.getRecords()) {
                ContentResult contentResult = new ContentResult();
                contentResult.setId(target.getId());
                contentResult.setTitle(target.getTitle());
                contentResult.setContent(target.getContent());
                contentResult.setCreateTime(Global.longToString(target.getCreateTime()));
                contentResult.setCreateAdmin(target.getCreateAdminName());
                results.add(contentResult);
            }
            result.setData(results);
            result.setPage(page);
            result.setLimit(limit);
            result.setTotal(iPages.getTotal());
            msg = "获取成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> addContent(Integer userId, String token, String title, String content) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, content), userId, token, "1102");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "创建失败";
        try {
            Content target = new Content();
            target.setTitle(title);
            target.setContent(content);
            target.setCreateTime(System.currentTimeMillis());
            target.setCreateAdmin(userId);
            baseMapper.insert(target);
            status = 1;
            msg = "创建成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> editContent(Integer userId, String token, Integer ids, String title, String content) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token, content), userId, token, "1102");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "更新失败";
        try {
            Content target = baseMapper.selectById(ids);
            if (target != null) {
                target.setTitle(title);
                target.setContent(content);
                baseMapper.updateById(target);
                status = 1;
                msg = "更新成功";
            } else msg = "文章不存在";
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    public ApiResult<Base> deleteContent(Integer userId, String token, Integer[] ids) {
        ApiResult<Base> apiResult = checkUtils.checkToken(Arrays.asList(userId, token), userId, token, "1104");
        if (apiResult.getStatus() != 1)
            return new ApiResult<>(apiResult.getStatus(), apiResult.getMsg(), null);
        ApiResult<Base> result = new ApiResult<>();
        int status = 0;
        String msg = "删除失败";
        try {
            for (Integer id : ids) {
                Content content = baseMapper.selectById(id);
                if (content == null) {
                    continue;
                }
                baseMapper.deleteById(id);
            }
            msg = "删除成功";
            status = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }
}
